﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Live;
using System.Data.Services.Client;
using Microsoft.Phone.Notification;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;
using System.IO.IsolatedStorage;

namespace NotificationReceiver
{
    public partial class SignIn : PhoneApplicationPage
    {
        const string ClientId = "000000004804274D";
        const string ClientSecret = "HcjYK1IHLA8HyCdG2JwGtCjjs1tokmrD";
        LiveDataContext dataContext = new LiveDataContext();
        AppInformation appInfo;
        Button signInButton;

      

        public SignIn()
        {
            InitializeComponent();
            this.dataContext.SignInCompleted += new SignInCompletedEventHandler(SignInCompleted);
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
           

           
        }

        //windows live login
        #region Sign in to Windows Live

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            button.IsEnabled = false;
            signInButton = button;

            try
            {
                appInfo = new AppInformation(ClientId, ClientSecret);
                appInfo.RequestedOffers.Add(new Offer()
                {
                    Name = "WL_Contacts",
                    Action = "View"
                });
                appInfo.RequestedOffers.Add(new Offer()
                {
                    Name = "WL_Activities",
                    Action = "View"
                });
                appInfo.RequestedOffers.Add(new Offer()
                {
                    Name = "WL_Photos",
                    Action = "View"
                });

                this.dataContext.SignInAsync(this.SignInBrowser, appInfo, null);
            }
            catch (Exception ex)
            {
                ShowErrorDialog("Failed to sign in to Windows Live: {0}", ex.Message);
                button.IsEnabled = true;
            }
        }

        private void SignInCompleted(object sender, SignInCompletedEventArgs e)
        {
            if (!e.Succeeded)
            {
                string authError = (e.AuthInfo.Error == null) ? string.Empty : e.AuthInfo.Error.Message;
                ShowErrorDialog("Failed to sign in to Windows Live! {0}", authError);
                signInButton.IsEnabled = true;
            }
            else
            {
                AppSettings apps = new AppSettings();
                apps.AunthicatedUserIdSetting = e.AuthInfo.UserId;
                (App.Current as App).SubscribeforNotification(e.AuthInfo.UserId);
                MessageBox.Show("Welcome To Windows Live!", "Windows Live", MessageBoxButton.OK);

                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            }
        }

        private static void ShowErrorDialog(string errMsg, params string[] paras)
        {
            MessageBox.Show(string.Format(errMsg, paras), "Windows Live", MessageBoxButton.OK);
        }

        #endregion   

       
    }
}